package com.fly.hbn.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fly.hbn.dao.CountryDAO;
import com.fly.hbn.model.Country;

@Repository
public class CountryDAOImpl implements CountryDAO
{
    @Autowired
    private SessionFactory sessionFactory;
    
    protected Session getSession()
    {
        return sessionFactory.getCurrentSession();
    }
    
    /**
     * @return
     */
    @Override
    public List<Country> getAllCountries()
    {
        List<Country> countryList = getSession().createQuery("from Country Order by id asc").list();
        return countryList;
    }
    
    /**
     * @param id
     * @return
     */
    @Override
    public Country getCountry(int id)
    {
        return getSession().get(Country.class, id);
    }
    
    /**
     * @param country
     * @return
     */
    @Override
    public Country addCountry(Country country)
    {
        getSession().persist(country);
        return country;
    }
    
    /**
     * @param country
     */
    @Override
    public void updateCountry(Country country)
    {
        getSession().update(country);
    }
    
    /**
     * @param id
     */
    @Override
    public void deleteCountry(int id)
    {
        Session session = getSession();
        Country p = session.get(Country.class, id);
        if (p != null)
        {
            session.delete(p);
        }
    }
}
